package com.cpic.blood.core.utils;

import java.sql.Time;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * <p><b>Title：</b> 日期工具类</p>
 * <p><b>Description：</b></p>
 *
 * @author 闫洪磊
 */
public class DateUtil {

        public final static String FORMAT_DATE = "yyyy-MM-dd";
        public final static String FORMAT_DATETIME = "yyyy-MM-dd HH:mm:ss";

        public final static String FORMAT_DATE_ZH = "yyyy年MM月dd日";
        public final static String FORMAT_DATETIME_ZH = "yyyy年MM月dd日 HH时mm分ss秒";

        public final static String TYPE_DATE = "date";
        public final static String TYPE_DATETIME = "datetime";
        
        public final static String FORMAT_SIMPLE_DATETIME = "yyyyMMddHHmm";
        
        public final static String FORMAT_SIMPLE_FORMAT_DATETIME = "yyyy-MM-dd HH:mm";
        
        public static final String YMD = "yyyyMMdd";
        public static final String YMDHM = "yyyyMMddHHmm";
        public static final String YMDHMS = "yyyyMMddHHmmss";
        public static final String ymd = "yyyy/MM/dd";
        public static final String ymd_HM = "yyyy/MM/dd HH:mm";
        public static final String ymd_HMS = "yyyy/MM/dd HH:mm:ss";


        /**
         * 日期排序类型-升序
         */
        public final static int DATE_ORDER_ASC = 0;

        /**
         * 日期排序类型-降序
         */
        public final static int DATE_ORDER_DESC = 1;

        /**
         * 用字符串获得日期
         * @throws ParseException
         * @dateValue 日期字符串
         * @dateType 格式化的类型,date和datetime
         */
        public static Date getDate(String dateValue, String dateType) throws ParseException {
                if (dateValue == null)
                        return null;
                if (dateType.equals(TYPE_DATE)) {
                        SimpleDateFormat sfdate = new SimpleDateFormat(FORMAT_DATE);
                        return sfdate.parse(dateValue);
                } else if (dateType.equals(TYPE_DATETIME)) {
                        SimpleDateFormat sftime = new SimpleDateFormat(FORMAT_DATETIME);
                        return sftime.parse(dateValue);
                }else{
                	 SimpleDateFormat sftime = new SimpleDateFormat(dateType);
                     return sftime.parse(dateValue);
                }
        }

        /**
         * 用字符串获得java.sql.Date日期
         * @throws ParseException
         * @dateValue 日期字符串
         * @dateType 格式化的类型,date和datetime
         */
        public static java.sql.Date getSqlDate(String dateValue, String dateType) throws ParseException {
                Date date = getDate(dateValue, dateType);
                if (date == null) {
                        return null;
                }
                return new java.sql.Date(date.getTime());
        }

        /**
         *将日期加上某些天或减去天数)返回字符串
         * @param date 待处理日期
         * @param to 加减的天数
         * @return 日期
         */
        public static Date dateAdd(String date, int to) {
                Date d = null;
                try {
                        d = java.sql.Date.valueOf(date);
                } catch (Exception e) {
                        e.printStackTrace();
                        d = new Date();
                }
                Calendar strDate = Calendar.getInstance();
                strDate.setTime(d);
                strDate.add(Calendar.DATE, to); // 日期减 如果不够减会将月变动
                return strDate.getTime();
        }

        /**
         *将日期加上某些天或减去天数)返回字符串
         * @param date 待处理日期
         * @param to 加减的天数
         * @return 日期
         */
        public static java.sql.Date dateAdd(java.sql.Date date, int to) {
                Calendar strDate = Calendar.getInstance();
                strDate.setTime(date);
                strDate.add(Calendar.DATE, to); // 日期减 如果不够减会将月变动
                return new java.sql.Date(strDate.getTime().getTime());
        }

        /**
         * 方法名:           setDayTime
         * 方法功能描述:       将日期设置到一天的最后，23点59分59秒
         * @param:         @param date
         * @param:         @return
         * @return:        Date
         * @Author:        陈龙
         * @Create Date:   2014年3月25日 下午3:32:55
         */
         public static Date setDaytimeLast(Date date){
         	Calendar calendar = Calendar.getInstance();
     		calendar.setTime(date);
     		calendar.set(Calendar.HOUR_OF_DAY, 23);
     		calendar.set(Calendar.MINUTE, 59);
     		calendar.set(Calendar.SECOND, 59);
     		return calendar.getTime();
         }


        /**
         * 格式化日期
         * @param date          日期对象
         * @param splitChar     分隔字符
         * @return
         */
        public static String formatDate(Date date, String splitChar) {
                SimpleDateFormat sfdate = new SimpleDateFormat("yyyy" + splitChar
                                + "MM" + splitChar + "dd");
                return sfdate.format(date);
        }

        /**
         * 格式化日期
         * @param date          日期对象
         * @param splitChar     分隔字符
         * @return
         */
        public static String formatDateNormal(Date date, String splitChar) {
        	SimpleDateFormat sfdate = new SimpleDateFormat(splitChar);
        	return sfdate.format(date);
        }

        /**
         * @dateValue 日期对象，可以是java.util.Date和java.sql.Date
         * @dateType 格式化的类型,date和datetime
         */
        public static String format(Object dateValue, String dateType) {
                if (dateValue == null)
                        return "";
                if (dateValue instanceof java.sql.Date) {
                        return dateValue.toString();
                } else if (dateValue instanceof Date) {
                        if (dateType.equals(TYPE_DATE)) {
                                SimpleDateFormat sfdate = new SimpleDateFormat(FORMAT_DATE);
                                return sfdate.format(dateValue);
                        } else if (dateType.equals(TYPE_DATETIME)) {
                                SimpleDateFormat sftime = new SimpleDateFormat(FORMAT_DATETIME);
                                return sftime.format(dateValue);
                        } else {
                                return "非法日期格式[" + dateType + "]";
                        }
                } else {
                        return "非日期类型";
                }
        }

        /**
         * 转换日期对象为中文化日期
         * @dateValue 日期对象，可以是java.util.Date和java.sql.Date
         * @dateType 格式化的类型,date和datetime
         */
        public static String formatZh(Date dateValue, String dateType) {
                if (dateValue == null)
                        return "";
                if (dateValue instanceof java.sql.Date) {
                        return dateValue.toString();
                } else if (dateValue instanceof Date) {
                        if (dateType.equals(TYPE_DATE)) {
                                SimpleDateFormat sfdate = new SimpleDateFormat(FORMAT_DATE_ZH);
                                return sfdate.format(dateValue);
                        } else if (dateType.equals(TYPE_DATETIME)) {
                                SimpleDateFormat sftime = new SimpleDateFormat(FORMAT_DATETIME_ZH);
                                return sftime.format(dateValue);
                        } else {
                                return "非法日期格式[" + dateType + "]";
                        }
                } else {
                        return "非日期类型";
                }
        }

        /**
           * 转化成年月日期
           * @param sDate          字符型日期：2009-02-02
           * @param DelimeterChar  分割符号比如 / -
           * @return               年月日期 :2009年02月02日
           */
        public static String chDateChange(String sDate, String DelimeterChar) {
                String tmpArr[] = sDate.split(DelimeterChar);
                tmpArr[0] = tmpArr[0] + "年";
                tmpArr[1] = tmpArr[1] + "月";
                tmpArr[2] = tmpArr[2] + "日";
                return tmpArr[0] + tmpArr[1] + tmpArr[2];
        }

        /**
         * 得到系统日期
         * @return YYYY-MM-DD
         */
        public static String getSysdate() {
                java.sql.Timestamp timeNow = new java.sql.Timestamp(System.currentTimeMillis());
                return timeNow.toString().substring(0, 10);
        }

        /**
         * 得到系统日期
         * @return YYYY-MM-DD
         */
        public static String getSysdate(String formatType) {
                java.sql.Timestamp timeNow = new java.sql.Timestamp(System.currentTimeMillis());
                return formatZh(timeNow, formatType);
        }

        /**
         * 得到某天是周几
         * @param strDay
         * @return 周几
         */
        public static int getWeekDay(String strDay) {
                Date day = DateUtil.dateAdd(strDay, -1);
                Calendar strDate = Calendar.getInstance();
                strDate.setTime(day);
                int meStrDate = strDate.get(Calendar.DAY_OF_WEEK);
                return meStrDate;
        }

        /**
         * 得到某天是周几
         * @param strDay
         * @return 周几
         */
        public static int getWeekDay(Date date) {
                Date day = DateUtil.dateAdd(format(date, "date"), -1);
                Calendar strDate = Calendar.getInstance();
                strDate.setTime(day);
                int meStrDate = strDate.get(Calendar.DAY_OF_WEEK);
                return meStrDate;
        }

        /**
         * 取得两个日期段的日期间隔
         *
         * @author color
         * @param t1 时间1
         * @param t2 时间2
         * @return t2 与t1的间隔天数
         * @throws ParseException
         *             如果输入的日期格式不是0000-00-00 格式抛出异常
         */
        public static int getBetweenDays(String t1, String t2) throws ParseException {
                DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                int betweenDays = 0;
                Date d1 = format.parse(t1);
                Date d2 = format.parse(t2);
                betweenDays = getBetweenDays(d1, d2);
                return betweenDays;
        }

        /**
         * 取得两个日期段的日期间隔
         *
         * @author color
         * @param t1 时间1
         * @param t2 时间2
         * @param swapDate      当日期1小于日期2时是否交换两个日期值
         * @return t2 与t1的间隔天数
         * @throws ParseException
         *             如果输入的日期格式不是0000-00-00 格式抛出异常
         */
        public static int getBetweenDays(String t1, String t2, boolean swapDate) throws ParseException {
                DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                int betweenDays = 0;
                Date d1 = format.parse(t1);
                Date d2 = format.parse(t2);
                betweenDays = getBetweenDays(d1, d2, swapDate);
                return betweenDays;
        }

        /**
         * 取得两个日期段的日期间隔
         * @param d1    日期1
         * @param d2    日期2
         * @param swapDate      当日期1小于日期2时是否交换两个日期值
         * @return      t2 与t1的间隔天数
         */
        public static int getBetweenDays(Date d1, Date d2, boolean swapDate) {
                if (d1 == null || d2 == null) {
                        return -1;
                }
                int betweenDays;
                Calendar c1 = Calendar.getInstance();
                Calendar c2 = Calendar.getInstance();
                c1.setTime(d1);
                c2.setTime(d2);
                if (swapDate) {
                        // 保证第二个时间一定大于第一个时间
                        if (c1.after(c2)) {
                                c2.setTime(d1);
                                c1.setTime(d2);
                        }
                }
                int betweenYears = c2.get(Calendar.YEAR) - c1.get(Calendar.YEAR);
                betweenDays = c2.get(Calendar.DAY_OF_YEAR) - c1.get(Calendar.DAY_OF_YEAR);
                for (int i = 0; i < betweenYears; i++) {
                        c1.set(Calendar.YEAR, (c1.get(Calendar.YEAR) + 1));
                        betweenDays += c1.getMaximum(Calendar.DAY_OF_YEAR);
                }
                return betweenDays;
        }

        /**
         * 取得两个日期段的日期间隔
         * @param d1    日期1
         * @param d2    日期2
         * @return      t2 与t1的间隔天数
         */
        private static int getBetweenDays(Date d1, Date d2) {
                if (d1 == null || d2 == null) {
                        return -1;
                }
                int betweenDays;
                Calendar c1 = Calendar.getInstance();
                Calendar c2 = Calendar.getInstance();
                c1.setTime(d1);
                c2.setTime(d2);
                // 保证第二个时间一定大于第一个时间
                if (c1.after(c2)) {
                        c2.setTime(d1);
                        c1.setTime(d2);
                }
                int betweenYears = c2.get(Calendar.YEAR) - c1.get(Calendar.YEAR);
                betweenDays = c2.get(Calendar.DAY_OF_YEAR) - c1.get(Calendar.DAY_OF_YEAR);
                for (int i = 0; i < betweenYears; i++) {
                        c1.set(Calendar.YEAR, (c1.get(Calendar.YEAR) + 1));
                        betweenDays += c1.getMaximum(Calendar.DAY_OF_YEAR);
                }
                return betweenDays;
        }

        /**
         * 判断指定日期是否在一个日期范围内
         * @param fromDate      范围开始日期
         * @param toDate        范围结束日期
         * @param testDate      测试日期
         * @return      在范围内true,否则false
         */
        public static boolean betweenDays(java.sql.Date fromDate, java.sql.Date toDate, java.sql.Date testDate) {
                if (fromDate == null || toDate == null || testDate == null) {
                        return false;
                }

                //1、 交换开始和结束日期
                if (fromDate.getTime() > toDate.getTime()) {
                        java.sql.Date tempDate = fromDate;
                        fromDate = toDate;
                        toDate = tempDate;
                }

                //2、缩小范围
                long testDateTime = testDate.getTime();
                if ( (testDateTime > fromDate.getTime() && testDateTime > toDate.getTime())
                                || testDateTime < fromDate.getTime() && testDateTime < toDate.getTime()) {
                        return false;
                }

                return true;
        }


        /**
         * 得到指定年、月的最后一天
         * @param year  年
         * @param month 月
         * @return      本年月的最后一天，如果2009,10，返回结果：2009-10-31
         */
        public static String getLastDateDayOfMonth(int year, int month) {
                Calendar cal = Calendar.getInstance();
                cal.set(Calendar.YEAR, year);
                cal.set(Calendar.MONTH, month);
                // 某年某月的最后一天
                int lastDate = cal.getActualMaximum(Calendar.DATE);
                return year + "-" + (month + 1) + "-" + lastDate;
        }

        /**
         * 判断两个日期是否为同一天
         * @param d1    日期一
         * @param d2    日期二
         * @return      同一天true，不是同一天false
         */
        public static boolean isSameDate(Date d1, Date d2) {
                boolean result = false;
                Calendar c1 = Calendar.getInstance();
                c1.setTime(d1);

                Calendar c2 = Calendar.getInstance();
                c2.setTime(d2);

                if (c1.get(Calendar.YEAR) == c2.get(Calendar.YEAR)
                                && c1.get(Calendar.MONTH) == c2.get(Calendar.MONTH)
                                && c1.get(Calendar.DAY_OF_MONTH) == c2.get(Calendar.DAY_OF_MONTH)) {
                        result = true;
                }
                return result;
        }

        /**
         * 获取当前系统时间，24小时制
         * @return      当前系统时间
         */
        public static Time getSystemTime() {
                Calendar c1 = Calendar.getInstance();
                int hour = c1.get(Calendar.HOUR_OF_DAY);
                int minute = c1.get(Calendar.MINUTE);
                int second = c1.get(Calendar.SECOND);
                Time systemTime = Time.valueOf(hour + ":" + minute + ":" + second);
                return systemTime;
        }

        /**
         * 是否为周末
         * @param strDate
         * @return true|false
         */
        public static boolean isWeekend(String strDate) {
                int weekDay = getWeekDay(strDate);
                if (weekDay == 6 || weekDay == 7) {
                        return true;
                } else {
                        return false;
                }
        }

        /**
         * 是否为周末
         * @param strDate
         * @return true|false
         */
        public static boolean isWeekend(Date date) {
                int weekDay = getWeekDay(format(date, "date"));
                if (weekDay == 6 || weekDay == 7) {
                        return true;
                } else {
                        return false;
                }
        }

        /**
         * 是否为法定节假日
         * @param strDate
         * @return true|false
         */
        public static boolean isHoliday(String strDate) {
                return false;
        }

        /**
         * 日期排序
         * @param dates         日期列表
         * @param orderType     排序类型
         *                      <br/>{@link #DATE_ORDER_ASC}<br/>
         *                      {@link #DATE_ORDER_DESC}
         * @return      排序结果
         */
        public static List<? extends Date> orderDate(List<? extends Date> dates, int orderType) {
                DateComparator comp = new DateComparator(orderType);
                Collections.sort(dates, comp);
                return dates;
        }

        /**
         * 日期分组<br/>
         * 能够对指定日期列表按照连续性分组<br/>
         * 例如：[2010-01-15, 2010-01-16, 2010-01-17, 2010-01-20, 2010-01-21, 2010-01-25]<br/>
         * 分组结果为：<br/>
         * <ul>
         * <li>[2010-01-15, 2010-01-16, 2010-01-17]</li>
         * <li>[2010-01-20, 2010-01-21]</li>
         * <li>[2010-01-25]</li>
         * </ul>
         * @param dates 日期对象
         * @return      连续性分组结果
         */
        public static List<List<? extends Date>> groupDates(List<? extends Date> dates) {
                List<List<? extends Date>> result = new ArrayList<>();

                // 按照升序排序
                orderDate(dates, DateUtil.DATE_ORDER_ASC);

                // 临时结果
                List<Date> tempDates = null;

                // 上一组最后一个日期
                Date lastDate = null;

                // 当前读取日期
                Date cdate = null;
                for (int i = 0; i < dates.size(); i++) {
                        cdate = dates.get(i);

                        // 第一次增加
                        if (tempDates == null) {
                                tempDates = new ArrayList<>();
                                tempDates.add(cdate);
                                result.add(tempDates);
                        } else {
                                /**
                                 * 差距为1是继续在原有的列表中添加，大于1就是用新的列表
                                 */
                                lastDate = tempDates.get(tempDates.size() - 1);
                                int days = getBetweenDays(lastDate, cdate);
                                if (days == 1) {
                                        tempDates.add(cdate);
                                } else {
                                        tempDates = new ArrayList<>();
                                        tempDates.add(cdate);
                                        result.add(tempDates);
                                }
                        }

                }

                return result;
        }

        public static List<java.sql.Date> getBetweenDates(java.sql.Date fromDate, java.sql.Date toDate) {
                List<java.sql.Date> result = new ArrayList<>();
                // 如果开始日期大于结束日期交换
                if (toDate.getTime() < fromDate.getTime()) {
                        java.sql.Date tempDate = fromDate;
                        fromDate = toDate;
                        toDate = tempDate;
                }

                Calendar ca = Calendar.getInstance();
                while ( fromDate.getTime() <= toDate.getTime() ) {
                        ca.setTime(fromDate);
                        java.sql.Date tempDate = new java.sql.Date(ca.getTime().getTime());
                        result.add(tempDate);
                        ca.add(Calendar.DATE, 1);
                        fromDate = new java.sql.Date(ca.getTime().getTime());
                }

                return result;
        }
        
        /** 
        * 方法名:         getIntervalDays
        * 方法功能描述:     比较相差天数
        * @param:         
        * @return:        
        * @Author:        陈龙
        * @Create Date:   2015年7月3日 上午9:35:52
        */
        public static int getIntervalDays(Date fDate, Date oDate) {
            if (null == fDate || null == oDate) {
                return -1;
            }

            long intervalMilli = oDate.getTime() - fDate.getTime();
            return (int) (intervalMilli / (24 * 60 * 60 * 1000));

         }

        public static List<java.sql.Date> getAllDate(List<java.sql.Date[]> dateList) {
                List<java.sql.Date> result = new ArrayList<>();
                for (Object[] objs : dateList) {
                        if (objs[0] == null || objs[1] == null) {
                                continue;
                        }
                        java.sql.Date date1 = (java.sql.Date) objs[0];
                        java.sql.Date date2 = (java.sql.Date) objs[1];
                        List<java.sql.Date> betweenDates = getBetweenDates(date1, date2);
                        for (java.sql.Date date : betweenDates) {
                                if (!result.contains(date)) {
                                        result.add(date);
                                }
                        }
                }
                return result;
        }
       
        /**
         *  将出生日期与当前日期相减，获得年龄
         * @param birthdayDate
         * @return
         */
        public static int getAge(Date birthdayDate) {
                String formatCurrent = new SimpleDateFormat("yyyy-MM-dd").format(new Date());

                int firstCu = formatCurrent.indexOf("-");
                int lastCu = formatCurrent.lastIndexOf("-");
                String currentYearStr = formatCurrent.substring(0, firstCu);
                String currentMonthStr = formatCurrent.substring(firstCu + 1, lastCu);
                String currentDayStr = formatCurrent.substring(lastCu + 1);
                int currentYear = Integer.valueOf(currentYearStr);
                int currentMonth = Integer.valueOf(currentMonthStr);
                int currentDay = Integer.valueOf(currentDayStr);

                String formatBirthday = new SimpleDateFormat("yyyy-MM-dd").format(birthdayDate);

                int first = formatBirthday.indexOf("-");
                int last = formatBirthday.lastIndexOf("-");
                String birthYearStr = formatBirthday.substring(0, first);
                String birthMonthStr = formatBirthday.substring(first + 1, last);
                String birthDayStr = formatBirthday.substring(last + 1);

                int birthYear = Integer.valueOf(birthYearStr);
                int birthMonth = Integer.valueOf(birthMonthStr);
                int birthDay = Integer.valueOf(birthDayStr);

                if (currentMonth > birthMonth) {
                        return  currentYear-birthYear;
                } else if (currentMonth == birthMonth) {
                        if (currentDay >= birthDay) {
                                return currentYear-birthYear;
                        } else {
                                return currentYear-birthYear - 1;
                        }
                } else {
                        return currentYear-birthYear - 1;
                }
        }
        
        /** 
        * 方法名:         getSimpleDateTime
        * 方法功能描述:     获取简单格式的当前时间
        * @param:         
        * @return:        
        * @Author:        陈龙
        * @Create Date:   2014年11月4日 下午3:30:18
        */
        public static String getSimpleDateTime(){
            SimpleDateFormat sfdate = new SimpleDateFormat(DateUtil.FORMAT_SIMPLE_DATETIME);
            return sfdate.format(new Date());
        }
        
        /** 
         * 方法名:         getSimpleDateTime
         * 方法功能描述:     获取简单格式的当前时间
         * @param:         
         * @return:        
         * @Author:        陈龙
         * @Create Date:   2014年11月4日 下午3:30:18
         */
        public static String getSimpleFormateDateTime(){
        	SimpleDateFormat sfdate = new SimpleDateFormat(DateUtil.FORMAT_SIMPLE_FORMAT_DATETIME);
        	return sfdate.format(new Date());
        }

        /**
         * yyyy-MM-dd HH:mm:ss
         * @return
         */
        public static String getFormatDate(){
                SimpleDateFormat sfdate = new SimpleDateFormat(DateUtil.FORMAT_DATETIME);
                return sfdate.format(new Date());
        }
         
        /**
         * 获取年月，例如 201009
         * @param dateObj
         * @return
         */
        public static String getYearMonth(Date dateObj) {
                if (dateObj == null) {
                        return "";
                }
                Calendar ca = Calendar.getInstance();
                ca.setTime(dateObj);
                int month = ca.get(Calendar.MONTH) + 1;
                String strMonth = month < 10 ? ("0" + month) : String.valueOf(month);
                String yearMonth = ca.get(Calendar.YEAR) + strMonth;
                return yearMonth;
        }
       
        /**
         * 根据指定年月计算上月年月标示<br/>
         * @param yearMonth
         * @return 201010返回201009，201001返回200912
         */
        public static String getPreYearMonth(String yearMonth) {
                if (yearMonth.length() == 6) {
                        int year = Integer.valueOf(yearMonth.substring(0, 4));
                        int month = Integer.valueOf(yearMonth.substring(4));
                        if (month != 1) {
                                month -= 1;
                        } else {
                                year -= 1;
                                month = 12;
                        }
                        return year + (month < 10 ? "0" + month : String.valueOf(month));
                }
                return "";
        }
       
        /**
         * 获取当前年份
         */
        public static Integer getCurrentYear() {
                Calendar ca = Calendar.getInstance();
                return ca.get(Calendar.YEAR);
        }
       
        /**
         * 获取当前月份
         */
        public static Integer getCurrentMonth() {
                Calendar ca = Calendar.getInstance();
                return ca.get(Calendar.MONTH) + 1;
        }
       
        public static void main(String[] args) {
                String preYearMonth = getPreYearMonth("201001");
                System.out.println(preYearMonth);
        }
        
        /**
         * 智能转换日期
         *
         * @param date
         * @return
         */
        public static String smartFormat(Date date) {
            String dateStr = null;
            if (date == null) {
                dateStr = "";
            } else {
                try {
                    dateStr = formatDate(date, DateUtil.FORMAT_DATETIME);
                    //时分秒
                    if (dateStr.endsWith(" 00:00:00")) {
                        dateStr = dateStr.substring(0, 10);
                    }
                    //时分
                    else if (dateStr.endsWith("00:00")) {
                        dateStr = dateStr.substring(0, 16);
                    }
                    //秒
                    else if (dateStr.endsWith(":00")) {
                        dateStr = dateStr.substring(0, 16);
                    }
                } catch (Exception ex) {
                    throw new IllegalArgumentException("转换日期失败: " + ex.getMessage(), ex);
                }
            }
            return dateStr;
        }

        /**
         * 智能转换日期
         *
         * @param text
         * @return
         */
        public static Date smartFormat(String text) {
            Date date = null;
            try {
                if (text == null || text.length() == 0) {
                    date = null;
                } else {
                    int length = text.length();
                    if (text.indexOf("-") > 0) {
                        if (length == 10) {
                            date = formatStringToDate(text,DateUtil.FORMAT_DATE);
                        } else if (length == 16) {
                            date = formatStringToDate(text, DateUtil.FORMAT_SIMPLE_FORMAT_DATETIME);
                        } else if (length == 19) {
                            date = formatStringToDate(text, DateUtil.FORMAT_DATETIME);
                        } else {
                            throw new IllegalArgumentException("日期长度不符合要求!");
                        }
                    } else if (text.indexOf("/") > 0) {
                        if (length == 10) {
                            date = formatStringToDate(text, DateUtil.ymd);
                        } else if (length == 16) {
                            date = formatStringToDate(text, DateUtil.ymd_HM);
                        } else if (length == 19) {
                            date = formatStringToDate(text, DateUtil.ymd_HMS);
                        } else {
                            throw new IllegalArgumentException("日期长度不符合要求!");
                        }
                    } else {
                        if (length == 10) {
                            date = formatStringToDate(text, DateUtil.YMD);
                        } else if (length == 13) {
                            date = new Date(Long.parseLong(text));
                        } else if (length == 16) {
                            date = formatStringToDate(text, DateUtil.YMDHM);
                        } else if (length == 19) {
                            date = formatStringToDate(text, DateUtil.YMDHMS);
                        } else {
                            throw new IllegalArgumentException("日期长度不符合要求!");
                        }
                    }
                }
            } catch (Exception e) {
                throw new IllegalArgumentException("日期格式错误!");
            }
            return date;
        }

        /**
         * 把字符串格式化成日期
         *
         * @param argDateStr
         * @param argFormat
         * @return
         */
        public static Date formatStringToDate(String argDateStr, String argFormat) throws Exception {
            if (argDateStr == null || argDateStr.trim().length() < 1) {
                throw new Exception("参数[日期]不能为空!");
            }
            String strFormat = argFormat;
            if (strFormat == null || strFormat.length() == 0) {
                strFormat = DateUtil.FORMAT_DATE;
                if (argDateStr.length() > 16) {
                    strFormat = DateUtil.FORMAT_DATETIME;
                } else if (argDateStr.length() > 10) {
                    strFormat = DateUtil.FORMAT_SIMPLE_FORMAT_DATETIME;
                }
            }
            SimpleDateFormat sdfFormat = new SimpleDateFormat(strFormat);
            //严格模式
            sdfFormat.setLenient(false);
            try {
                return sdfFormat.parse(argDateStr);
            } catch (ParseException e) {
                throw new Exception(e);
            }
        }
       
}

/**
 * <p><b>Title：</b>日期大小比较</p>
 * <p><b>Description：</b>实现比较接口，按照排序类型[升序,降序]排列日期集合</p>
 *
 * @author 闫洪磊
 */
class DateComparator implements Comparator<Date> {

        int orderType;

        public DateComparator(int orderType) {
                this.orderType = orderType;
        }

        @Override
		public int compare(Date d1, Date d2) {
                if (d1.getTime() > d2.getTime()) {
                        if (orderType == DateUtil.DATE_ORDER_ASC) {
                                return 1;
                        } else {
                                return -1;
                        }
                } else {
                        if (d1.getTime() == d2.getTime()) {
                                return 0;
                        } else {
                                if (orderType == DateUtil.DATE_ORDER_DESC) {
                                        return 1;
                                } else {
                                        return -1;
                                }
                        }
                }
        }
}



